package com.hn.sanya.zzht.controller;

import com.auth0.jwt.internal.org.bouncycastle.asn1.ocsp.ResponseData;
import com.hn.sanya.zzht.entity.User;
import com.hn.sanya.zzht.model.ResponseStatus;
import com.hn.sanya.zzht.service.UserService;
import com.hn.sanya.zzht.util.jwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
　　* @Description: 测试控制器
　  * @author syiti
    * @Method
　　* @params
　　* @return
　　* @date 2018/3/9 11:24
　　*/
@Api(value = "测试", description = "测试描述")
@RestController
@RequestMapping(value ="/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserService userService;

   @ApiOperation( value = "获取前端json数据", notes = "获取前端json数据", httpMethod = "POST" )
   @ApiImplicitParam(name = "data", value = "json数据", required = true, dataType = "JSON", paramType = "path")
   @RequestMapping(value = "getTableData", method = RequestMethod.POST, consumes="application/json")
   public ResponseStatus getTableData(
           @ApiParam(name = "data", value = "json数据", required = false)  @RequestBody String data,HttpServletRequest request, HttpServletResponse response) {

       ResponseStatus responseStatus = ResponseStatus.ok();

       try{
           request.setCharacterEncoding("UTF-8");
           //先到数据库验证（这一步根据自己的代码逻辑来实现）
          // User user = service.findUserByNameAndPassword(userName.trim(),CommUtil.generateMD5Str(password));
           User user=userService.getFindUserInfo("linxihao");
           if(null != user) {
               //给用户jwt加密生成token 这里是关键，生成了token 后，怎么给前台返回，那可以用你自己的方法；
               String token = jwtUtil.sign(user);
               //封装成对象返回给客户端
               responseStatus.putDataValue("token", token);
               responseStatus.putDataValue("user", user);
               request.getSession().setAttribute("token",token);
           } else{
               responseStatus = ResponseStatus.customerError();
           }
       }catch (Exception e){
           e.printStackTrace();
       }
       return responseStatus;

   }


    //跳转到登录页面
    @ApiOperation( value = "用户登录", notes = "通过用户名登录", httpMethod = "GET" )
    @RequestMapping(value = "loginCheck",method = RequestMethod.GET)
    public JSONObject loginCheck(){ //@ApiParam(name = "data", value = "json数据", required = false) @PathVariable String val

        JSONObject json=new JSONObject();
        User user=userService.getFindUserInfo("linxihao");

        if(user==null){
            json.put("data",null);
        }else {
            json.put("data",JSONObject.fromObject(user));
        }
        return json;
    }
}
